<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang = "en">

<head>

<link rel="shortcut icon" href="http://algs4.cs.princeton.edu/favicon.ico">
<link rel="stylesheet"    href="http://algs4.cs.princeton.edu/java.css" type="text/css">

<title>StdRandom.java</title>

<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<meta NAME="AUTHOR" CONTENT="Robert Sedgewick and Kevin Wayne">
<meta NAME="DESCRIPTION" CONTENT="StdRandom code in Java">
<meta NAME="TITLE" CONTENT="StdRandom code in Java">
<meta NAME="KEYWORDS" CONTENT="StdRandom,java,programming,computer science,algorithm,data structure,program,code">
<meta NAME="ROBOTS" CONTENT="INDEX,FOLLOW">

</head>


<body>
<center><h1>StdRandom.java</h1></center><p><br>

Below is the syntax highlighted version of <a href = "StdRandom.java">StdRandom.java</a>.
<p><br>

<!-- Generator: GNU source-highlight 3.1.6
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span class="comment">/******************************************************************************</span>
<span class="comment"> *  Compilation:  javac StdRandom.java</span>
<span class="comment"> *  Execution:    java StdRandom</span>
<span class="comment"> *  Dependencies: StdOut.java</span>
<span class="comment"> *</span>
<span class="comment"> *  A library of static methods to generate pseudo-random numbers from</span>
<span class="comment"> *  different distributions (bernoulli, uniform, gaussian, discrete,</span>
<span class="comment"> *  and exponential). Also includes a method for shuffling an array.</span>
<span class="comment"> *</span>
<span class="comment"> *</span>
<span class="comment"> *  %  java StdRandom 5</span>
<span class="comment"> *  seed = 1316600602069</span>
<span class="comment"> *  59 16.81826  true 8.83954  0 </span>
<span class="comment"> *  32 91.32098  true 9.11026  0 </span>
<span class="comment"> *  35 10.11874  true 8.95396  3 </span>
<span class="comment"> *  92 32.88401  true 8.87089  0 </span>
<span class="comment"> *  72 92.55791  true 9.46241  0 </span>
<span class="comment"> *</span>
<span class="comment"> *  % java StdRandom 5</span>
<span class="comment"> *  seed = 1316600616575</span>
<span class="comment"> *  96 60.17070  true 8.72821  0 </span>
<span class="comment"> *  79 32.01607  true 8.58159  0 </span>
<span class="comment"> *  81 59.49065  true 9.10423  1 </span>
<span class="comment"> *  96 51.65818  true 9.02102  0 </span>
<span class="comment"> *  99 17.55771  true 8.99762  0 </span>
<span class="comment"> *</span>
<span class="comment"> *  % java StdRandom 5 1316600616575</span>
<span class="comment"> *  seed = 1316600616575</span>
<span class="comment"> *  96 60.17070  true 8.72821  0 </span>
<span class="comment"> *  79 32.01607  true 8.58159  0 </span>
<span class="comment"> *  81 59.49065  true 9.10423  1 </span>
<span class="comment"> *  96 51.65818  true 9.02102  0 </span>
<span class="comment"> *  99 17.55771  true 8.99762  0 </span>
<span class="comment"> *</span>
<span class="comment"> *</span>
<span class="comment"> *  Remark</span>
<span class="comment"> *  ------</span>
<span class="comment"> *    - Relies on randomness of nextDouble() method in java.util.Random</span>
<span class="comment"> *      to generate pseudorandom numbers in [0, 1).</span>
<span class="comment"> *</span>
<span class="comment"> *    - This library allows you to set and get the pseudorandom number seed.</span>
<span class="comment"> *</span>
<span class="comment"> *    - See </span><span class="url">http://www.honeylocust.com/RngPack/</span><span class="comment"> for an industrial</span>
<span class="comment"> *      strength random number generator in Java.</span>
<span class="comment"> *</span>
<span class="comment"> ******************************************************************************/</span>

<span class="preproc">package</span><span class="normal"> edu</span><span class="symbol">.</span><span class="normal">princeton</span><span class="symbol">.</span><span class="normal">cs</span><span class="symbol">.</span><span class="normal">algs4</span><span class="symbol">;</span>

<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">util</span><span class="symbol">.</span><span class="normal">Random</span><span class="symbol">;</span>

<span class="comment">/**</span>
<span class="comment"> *  The {</span><span class="type">@code</span><span class="comment"> StdRandom} class provides static methods for generating</span>
<span class="comment"> *  random number from various discrete and continuous distributions, </span>
<span class="comment"> *  including Bernoulli, uniform, Gaussian, exponential, pareto,</span>
<span class="comment"> *  Poisson, and Cauchy. It also provides method for shuffling an</span>
<span class="comment"> *  array or subarray.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  For additional documentation,</span>
<span class="comment"> *  see </span><span class="keyword">&lt;a</span><span class="normal"> </span><span class="type">href</span><span class="symbol">=</span><span class="string">"http://introcs.cs.princeton.edu/22library"</span><span class="keyword">&gt;</span><span class="comment">Section 2.2</span><span class="keyword">&lt;/a&gt;</span><span class="comment"> of</span>
<span class="comment"> *  </span><span class="keyword">&lt;i&gt;</span><span class="comment">Introduction to Programming in Java: An Interdisciplinary Approach</span><span class="keyword">&lt;/i&gt;</span>
<span class="comment"> *  by Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Robert Sedgewick</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Kevin Wayne</span>
<span class="comment"> */</span>
<span class="keyword">public</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">StdRandom</span><span class="normal"> </span><span class="cbracket">{</span>

<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="usertype">Random</span><span class="normal"> random</span><span class="symbol">;</span><span class="normal">    </span><span class="comment">// pseudo-random number generator</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">long</span><span class="normal"> seed</span><span class="symbol">;</span><span class="normal">        </span><span class="comment">// pseudo-random number generator seed</span>

<span class="normal">    </span><span class="comment">// static initializer</span>
<span class="normal">    </span><span class="keyword">static</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="comment">// this is how the seed was set in Java 1.4</span>
<span class="normal">        seed </span><span class="symbol">=</span><span class="normal"> System</span><span class="symbol">.</span><span class="function">currentTimeMillis</span><span class="symbol">();</span>
<span class="normal">        random </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Random</span><span class="symbol">(</span><span class="normal">seed</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">// don't instantiate</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="function">StdRandom</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Sets the seed of the pseudorandom number generator.</span>
<span class="comment">     * This method enables you to produce the same sequence of "random"</span>
<span class="comment">     * number for each execution of the program.</span>
<span class="comment">     * Ordinarily, you should call this method at most once per program.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> s the seed</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">setSeed</span><span class="symbol">(</span><span class="type">long</span><span class="normal"> s</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        seed   </span><span class="symbol">=</span><span class="normal"> s</span><span class="symbol">;</span>
<span class="normal">        random </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Random</span><span class="symbol">(</span><span class="normal">seed</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the seed of the pseudorandom number generator.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the seed</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">long</span><span class="normal"> </span><span class="function">getSeed</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> seed</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns a random real number uniformly in [0, 1).</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> a random real number uniformly in [0, 1)</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">uniform</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> random</span><span class="symbol">.</span><span class="function">nextDouble</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns a random integer uniformly in [0, n).</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> n number of possible integers</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> a random integer uniformly between 0 (inclusive) and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">N</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> (exclusive)</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if </span><span class="keyword">&lt;tt&gt;</span><span class="comment">n &lt;= 0</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">uniform</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> n</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">n </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Parameter N must be positive"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> random</span><span class="symbol">.</span><span class="function">nextInt</span><span class="symbol">(</span><span class="normal">n</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">///////////////////////////////////////////////////////////////////////////</span>
<span class="normal">    </span><span class="comment">//  STATIC METHODS BELOW RELY ON JAVA.UTIL.RANDOM ONLY INDIRECTLY VIA</span>
<span class="normal">    </span><span class="comment">//  THE STATIC METHODS ABOVE.</span>
<span class="normal">    </span><span class="comment">///////////////////////////////////////////////////////////////////////////</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns a random real number uniformly in [0, 1).</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment">     a random real number uniformly in [0, 1)</span>
<span class="comment">     * </span><span class="type">@deprecated</span><span class="comment"> Replaced by {</span><span class="type">@link</span><span class="comment"> #uniform()}.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">random</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">uniform</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns a random integer uniformly in [a, b).</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  a the left endpoint</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  b the right endpoint</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> a random integer uniformly in [a, b)</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if </span><span class="keyword">&lt;tt&gt;</span><span class="comment">b &lt;= a</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if </span><span class="keyword">&lt;tt&gt;</span><span class="comment">b - a &gt;= Integer.MAX_VALUE</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">uniform</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> a</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> b</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">b </span><span class="symbol">&lt;=</span><span class="normal"> a</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Invalid range"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">((</span><span class="type">long</span><span class="symbol">)</span><span class="normal"> b </span><span class="symbol">-</span><span class="normal"> a </span><span class="symbol">&gt;=</span><span class="normal"> Integer</span><span class="symbol">.</span><span class="normal">MAX_VALUE</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Invalid range"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> a </span><span class="symbol">+</span><span class="normal"> </span><span class="function">uniform</span><span class="symbol">(</span><span class="normal">b </span><span class="symbol">-</span><span class="normal"> a</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns a random real number uniformly in [a, b).</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  a the left endpoint</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  b the right endpoint</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> a random real number uniformly in [a, b)</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException unless </span><span class="keyword">&lt;tt&gt;</span><span class="comment">a &lt; b</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">uniform</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> a</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> b</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!(</span><span class="normal">a </span><span class="symbol">&lt;</span><span class="normal"> b</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Invalid range"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> a </span><span class="symbol">+</span><span class="normal"> </span><span class="function">uniform</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">*</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">b</span><span class="symbol">-</span><span class="normal">a</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns a random boolean from a Bernoulli distribution with success</span>
<span class="comment">     * probability </span><span class="keyword">&lt;em&gt;</span><span class="comment">p</span><span class="keyword">&lt;/em&gt;</span><span class="comment">.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  p the probability of returning </span><span class="keyword">&lt;tt&gt;</span><span class="comment">true</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">true</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> with probability </span><span class="keyword">&lt;tt&gt;</span><span class="comment">p</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> and</span>
<span class="comment">     *         </span><span class="keyword">&lt;tt&gt;</span><span class="comment">false</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> with probability </span><span class="keyword">&lt;tt&gt;</span><span class="comment">p</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException unless </span><span class="keyword">&lt;tt&gt;</span><span class="comment">p &gt;= 0.0</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">p &lt;= 1.0</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">bernoulli</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> p</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!(</span><span class="normal">p </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="number">0.0</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> p </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1.0</span><span class="symbol">))</span>
<span class="normal">            </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Probability must be between 0.0 and 1.0"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">uniform</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">&lt;</span><span class="normal"> p</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns a random boolean from a Bernoulli distribution with success</span>
<span class="comment">     * probability 1/2.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">true</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> with probability 1/2 and</span>
<span class="comment">     *         </span><span class="keyword">&lt;tt&gt;</span><span class="comment">false</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> with probability 1/2</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">bernoulli</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">bernoulli</span><span class="symbol">(</span><span class="number">0.5</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns a random real number from a standard Gaussian distribution.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> a random real number from a standard Gaussian distribution</span>
<span class="comment">     *         (mean 0 and standard deviation 1).</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">gaussian</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="comment">// use the polar form of the Box-Muller transform</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> r</span><span class="symbol">,</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> y</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">do</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            x </span><span class="symbol">=</span><span class="normal"> </span><span class="function">uniform</span><span class="symbol">(-</span><span class="number">1.0</span><span class="symbol">,</span><span class="normal"> </span><span class="number">1.0</span><span class="symbol">);</span>
<span class="normal">            y </span><span class="symbol">=</span><span class="normal"> </span><span class="function">uniform</span><span class="symbol">(-</span><span class="number">1.0</span><span class="symbol">,</span><span class="normal"> </span><span class="number">1.0</span><span class="symbol">);</span>
<span class="normal">            r </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">*</span><span class="normal">x </span><span class="symbol">+</span><span class="normal"> y</span><span class="symbol">*</span><span class="normal">y</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span><span class="normal"> </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">r </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> r </span><span class="symbol">==</span><span class="normal"> </span><span class="number">0</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> x </span><span class="symbol">*</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">sqrt</span><span class="symbol">(-</span><span class="number">2</span><span class="normal"> </span><span class="symbol">*</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">log</span><span class="symbol">(</span><span class="normal">r</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">/</span><span class="normal"> r</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// Remark:  y * Math.sqrt(-2 * Math.log(r) / r)</span>
<span class="normal">        </span><span class="comment">// is an independent random gaussian</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns a random real number from a Gaussian distribution with mean </span><span class="preproc">&amp;mu;</span>
<span class="comment">     * and standard deviation </span><span class="preproc">&amp;sigma;</span><span class="comment">.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  mu the mean</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  sigma the standard deviation</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> a real number distributed according to the Gaussian distribution</span>
<span class="comment">     *         with mean </span><span class="keyword">&lt;tt&gt;</span><span class="comment">mu</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> and standard deviation </span><span class="keyword">&lt;tt&gt;</span><span class="comment">sigma</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">gaussian</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> mu</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> sigma</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> mu </span><span class="symbol">+</span><span class="normal"> sigma </span><span class="symbol">*</span><span class="normal"> </span><span class="function">gaussian</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns a random integer from a geometric distribution with success</span>
<span class="comment">     * probability </span><span class="keyword">&lt;em&gt;</span><span class="comment">p</span><span class="keyword">&lt;/em&gt;</span><span class="comment">.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  p the parameter of the geometric distribution</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> a random integer from a geometric distribution with success</span>
<span class="comment">     *         probability </span><span class="keyword">&lt;tt&gt;</span><span class="comment">p</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">; or </span><span class="keyword">&lt;tt&gt;</span><span class="comment">Integer.MAX_VALUE</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if</span>
<span class="comment">     *         </span><span class="keyword">&lt;tt&gt;</span><span class="comment">p</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> is (nearly) equal to </span><span class="keyword">&lt;tt&gt;</span><span class="comment">1.0</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">.</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException unless </span><span class="keyword">&lt;tt&gt;</span><span class="comment">p &gt;= 0.0</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">p &lt;= 1.0</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">geometric</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> p</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!(</span><span class="normal">p </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="number">0.0</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> p </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1.0</span><span class="symbol">))</span>
<span class="normal">            </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Probability must be between 0.0 and 1.0"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="comment">// using algorithm given by Knuth</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="symbol">)</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">ceil</span><span class="symbol">(</span><span class="normal">Math</span><span class="symbol">.</span><span class="function">log</span><span class="symbol">(</span><span class="function">uniform</span><span class="symbol">())</span><span class="normal"> </span><span class="symbol">/</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">log</span><span class="symbol">(</span><span class="number">1.0</span><span class="normal"> </span><span class="symbol">-</span><span class="normal"> p</span><span class="symbol">));</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns a random integer from a Poisson distribution with mean </span><span class="preproc">&amp;lambda;</span><span class="comment">.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  lambda the mean of the Poisson distribution</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> a random integer from a Poisson distribution with mean </span><span class="keyword">&lt;tt&gt;</span><span class="comment">lambda</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException unless </span><span class="keyword">&lt;tt&gt;</span><span class="comment">lambda &gt; 0.0</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> and not infinite</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">poisson</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> lambda</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!(</span><span class="normal">lambda </span><span class="symbol">&gt;</span><span class="normal"> </span><span class="number">0.0</span><span class="symbol">))</span>
<span class="normal">            </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Parameter lambda must be positive"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">Double</span><span class="symbol">.</span><span class="function">isInfinite</span><span class="symbol">(</span><span class="normal">lambda</span><span class="symbol">))</span>
<span class="normal">            </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Parameter lambda must not be infinite"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="comment">// using algorithm given by Knuth</span>
<span class="normal">        </span><span class="comment">// see http://en.wikipedia.org/wiki/Poisson_distribution</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> k </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> p </span><span class="symbol">=</span><span class="normal"> </span><span class="number">1.0</span><span class="symbol">;</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> L </span><span class="symbol">=</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">exp</span><span class="symbol">(-</span><span class="normal">lambda</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">do</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            k</span><span class="symbol">++;</span>
<span class="normal">            p </span><span class="symbol">*=</span><span class="normal"> </span><span class="function">uniform</span><span class="symbol">();</span>
<span class="normal">        </span><span class="cbracket">}</span><span class="normal"> </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">p </span><span class="symbol">&gt;=</span><span class="normal"> L</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> k</span><span class="symbol">-</span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns a random real number from the standard Pareto distribution.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> a random real number from the standard Pareto distribution</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">pareto</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">pareto</span><span class="symbol">(</span><span class="number">1.0</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns a random real number from a Pareto distribution with</span>
<span class="comment">     * shape parameter </span><span class="preproc">&amp;alpha;</span><span class="comment">.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  alpha shape parameter</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> a random real number from a Pareto distribution with shape</span>
<span class="comment">     *         parameter </span><span class="keyword">&lt;tt&gt;</span><span class="comment">alpha</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException unless </span><span class="keyword">&lt;tt&gt;</span><span class="comment">alpha &gt; 0.0</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">pareto</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> alpha</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!(</span><span class="normal">alpha </span><span class="symbol">&gt;</span><span class="normal"> </span><span class="number">0.0</span><span class="symbol">))</span>
<span class="normal">            </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Shape parameter alpha must be positive"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">pow</span><span class="symbol">(</span><span class="number">1</span><span class="normal"> </span><span class="symbol">-</span><span class="normal"> </span><span class="function">uniform</span><span class="symbol">(),</span><span class="normal"> </span><span class="symbol">-</span><span class="number">1.0</span><span class="symbol">/</span><span class="normal">alpha</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">-</span><span class="normal"> </span><span class="number">1.0</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns a random real number from the Cauchy distribution.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> a random real number from the Cauchy distribution.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">cauchy</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">tan</span><span class="symbol">(</span><span class="normal">Math</span><span class="symbol">.</span><span class="normal">PI </span><span class="symbol">*</span><span class="normal"> </span><span class="symbol">(</span><span class="function">uniform</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">-</span><span class="normal"> </span><span class="number">0.5</span><span class="symbol">));</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns a random integer from the specified discrete distribution.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  probabilities the probability of occurrence of each integer</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> a random integer from a discrete distribution:</span>
<span class="comment">     *         </span><span class="keyword">&lt;tt&gt;</span><span class="comment">i</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> with probability </span><span class="keyword">&lt;tt&gt;</span><span class="comment">probabilities[i]</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> NullPointerException if </span><span class="keyword">&lt;tt&gt;</span><span class="comment">probabilities</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> is </span><span class="keyword">&lt;tt&gt;</span><span class="comment">null</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if sum of array entries is not (very nearly) equal to </span><span class="keyword">&lt;tt&gt;</span><span class="comment">1.0</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException unless </span><span class="keyword">&lt;tt&gt;</span><span class="comment">probabilities[i] &gt;= 0.0</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> for each index </span><span class="keyword">&lt;tt&gt;</span><span class="comment">i</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">discrete</span><span class="symbol">(</span><span class="type">double</span><span class="symbol">[]</span><span class="normal"> probabilities</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">probabilities </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">(</span><span class="string">"argument array is null"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> EPSILON </span><span class="symbol">=</span><span class="normal"> </span><span class="number">1E-14</span><span class="symbol">;</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> sum </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0.0</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> probabilities</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!(</span><span class="normal">probabilities</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="number">0.0</span><span class="symbol">))</span>
<span class="normal">                </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"array entry "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> i </span><span class="symbol">+</span><span class="normal"> </span><span class="string">" must be nonnegative: "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> probabilities</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]);</span>
<span class="normal">            sum </span><span class="symbol">+=</span><span class="normal"> probabilities</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">];</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">sum </span><span class="symbol">&gt;</span><span class="normal"> </span><span class="number">1.0</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> EPSILON </span><span class="symbol">||</span><span class="normal"> sum </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">1.0</span><span class="normal"> </span><span class="symbol">-</span><span class="normal"> EPSILON</span><span class="symbol">)</span>
<span class="normal">            </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"sum of array entries does not approximately equal 1.0: "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> sum</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// the for loop may not return a value when both r is (nearly) 1.0 and when the</span>
<span class="normal">        </span><span class="comment">// cumulative sum is less than 1.0 (as a result of floating-point roundoff error)</span>
<span class="normal">        </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(</span><span class="keyword">true</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="type">double</span><span class="normal"> r </span><span class="symbol">=</span><span class="normal"> </span><span class="function">uniform</span><span class="symbol">();</span>
<span class="normal">            sum </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0.0</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> probabilities</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                sum </span><span class="symbol">=</span><span class="normal"> sum </span><span class="symbol">+</span><span class="normal"> probabilities</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">];</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">sum </span><span class="symbol">&gt;</span><span class="normal"> r</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> i</span><span class="symbol">;</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns a random integer from the specified discrete distribution.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  frequencies the frequency of occurrence of each integer</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> a random integer from a discrete distribution:</span>
<span class="comment">     *         </span><span class="keyword">&lt;tt&gt;</span><span class="comment">i</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> with probability proportional to </span><span class="keyword">&lt;tt&gt;</span><span class="comment">frequencies[i]</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> NullPointerException if </span><span class="keyword">&lt;tt&gt;</span><span class="comment">frequencies</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> is </span><span class="keyword">&lt;tt&gt;</span><span class="comment">null</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if all array entries are </span><span class="keyword">&lt;tt&gt;</span><span class="comment">0</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if </span><span class="keyword">&lt;tt&gt;</span><span class="comment">frequencies[i]</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> is negative for any index </span><span class="keyword">&lt;tt&gt;</span><span class="comment">i</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if sum of frequencies exceeds </span><span class="keyword">&lt;tt&gt;</span><span class="comment">Integer.MAX_VALUE</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> (2</span><span class="keyword">&lt;sup&gt;</span><span class="comment">31</span><span class="keyword">&lt;/sup&gt;</span><span class="comment"> - 1)</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">discrete</span><span class="symbol">(</span><span class="type">int</span><span class="symbol">[]</span><span class="normal"> frequencies</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">frequencies </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">(</span><span class="string">"argument array is null"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">long</span><span class="normal"> sum </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> frequencies</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">frequencies</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span>
<span class="normal">                </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"array entry "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> i </span><span class="symbol">+</span><span class="normal"> </span><span class="string">" must be nonnegative: "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> frequencies</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]);</span>
<span class="normal">            sum </span><span class="symbol">+=</span><span class="normal"> frequencies</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">];</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">sum </span><span class="symbol">==</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span>
<span class="normal">            </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"at least one array entry must be positive"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">sum </span><span class="symbol">&gt;=</span><span class="normal"> Integer</span><span class="symbol">.</span><span class="normal">MAX_VALUE</span><span class="symbol">)</span>
<span class="normal">            </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"sum of frequencies overflows an int"</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// pick index i with probabilitity proportional to frequency</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> r </span><span class="symbol">=</span><span class="normal"> </span><span class="function">uniform</span><span class="symbol">((</span><span class="type">int</span><span class="symbol">)</span><span class="normal"> sum</span><span class="symbol">);</span>
<span class="normal">        sum </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> frequencies</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            sum </span><span class="symbol">+=</span><span class="normal"> frequencies</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">];</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">sum </span><span class="symbol">&gt;</span><span class="normal"> r</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> i</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// can't reach here</span>
<span class="normal">        </span><span class="keyword">assert</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">-</span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns a random real number from an exponential distribution</span>
<span class="comment">     * with rate </span><span class="preproc">&amp;lambda;</span><span class="comment">.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  lambda the rate of the exponential distribution</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> a random real number from an exponential distribution with</span>
<span class="comment">     *         rate </span><span class="keyword">&lt;tt&gt;</span><span class="comment">lambda</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException unless </span><span class="keyword">&lt;tt&gt;</span><span class="comment">lambda &gt; 0.0</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">exp</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> lambda</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!(</span><span class="normal">lambda </span><span class="symbol">&gt;</span><span class="normal"> </span><span class="number">0.0</span><span class="symbol">))</span>
<span class="normal">            </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Rate lambda must be positive"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">-</span><span class="normal">Math</span><span class="symbol">.</span><span class="function">log</span><span class="symbol">(</span><span class="number">1</span><span class="normal"> </span><span class="symbol">-</span><span class="normal"> </span><span class="function">uniform</span><span class="symbol">())</span><span class="normal"> </span><span class="symbol">/</span><span class="normal"> lambda</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Rearranges the elements of the specified array in uniformly random order.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  a the array to shuffle</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> NullPointerException if </span><span class="keyword">&lt;tt&gt;</span><span class="comment">a</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> is </span><span class="keyword">&lt;tt&gt;</span><span class="comment">null</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">shuffle</span><span class="symbol">(</span><span class="normal">Object</span><span class="symbol">[]</span><span class="normal"> a</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">a </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">(</span><span class="string">"argument array is null"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> n </span><span class="symbol">=</span><span class="normal"> a</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> n</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> r </span><span class="symbol">=</span><span class="normal"> i </span><span class="symbol">+</span><span class="normal"> </span><span class="function">uniform</span><span class="symbol">(</span><span class="normal">n</span><span class="symbol">-</span><span class="normal">i</span><span class="symbol">);</span><span class="normal">     </span><span class="comment">// between i and n-1</span>
<span class="normal">            </span><span class="usertype">Object</span><span class="normal"> temp </span><span class="symbol">=</span><span class="normal"> a</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">];</span>
<span class="normal">            a</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> a</span><span class="symbol">[</span><span class="normal">r</span><span class="symbol">];</span>
<span class="normal">            a</span><span class="symbol">[</span><span class="normal">r</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> temp</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Rearranges the elements of the specified array in uniformly random order.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  a the array to shuffle</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> NullPointerException if </span><span class="keyword">&lt;tt&gt;</span><span class="comment">a</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> is </span><span class="keyword">&lt;tt&gt;</span><span class="comment">null</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">shuffle</span><span class="symbol">(</span><span class="type">double</span><span class="symbol">[]</span><span class="normal"> a</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">a </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">(</span><span class="string">"argument array is null"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> n </span><span class="symbol">=</span><span class="normal"> a</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> n</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> r </span><span class="symbol">=</span><span class="normal"> i </span><span class="symbol">+</span><span class="normal"> </span><span class="function">uniform</span><span class="symbol">(</span><span class="normal">n</span><span class="symbol">-</span><span class="normal">i</span><span class="symbol">);</span><span class="normal">     </span><span class="comment">// between i and n-1</span>
<span class="normal">            </span><span class="type">double</span><span class="normal"> temp </span><span class="symbol">=</span><span class="normal"> a</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">];</span>
<span class="normal">            a</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> a</span><span class="symbol">[</span><span class="normal">r</span><span class="symbol">];</span>
<span class="normal">            a</span><span class="symbol">[</span><span class="normal">r</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> temp</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Rearranges the elements of the specified array in uniformly random order.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  a the array to shuffle</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> NullPointerException if </span><span class="keyword">&lt;tt&gt;</span><span class="comment">a</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> is </span><span class="keyword">&lt;tt&gt;</span><span class="comment">null</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">shuffle</span><span class="symbol">(</span><span class="type">int</span><span class="symbol">[]</span><span class="normal"> a</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">a </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">(</span><span class="string">"argument array is null"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> n </span><span class="symbol">=</span><span class="normal"> a</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> n</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> r </span><span class="symbol">=</span><span class="normal"> i </span><span class="symbol">+</span><span class="normal"> </span><span class="function">uniform</span><span class="symbol">(</span><span class="normal">n</span><span class="symbol">-</span><span class="normal">i</span><span class="symbol">);</span><span class="normal">     </span><span class="comment">// between i and n-1</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> temp </span><span class="symbol">=</span><span class="normal"> a</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">];</span>
<span class="normal">            a</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> a</span><span class="symbol">[</span><span class="normal">r</span><span class="symbol">];</span>
<span class="normal">            a</span><span class="symbol">[</span><span class="normal">r</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> temp</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Rearranges the elements of the specified subarray in uniformly random order.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  a the array to shuffle</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  lo the left endpoint (inclusive)</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  hi the right endpoint (inclusive)</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> NullPointerException if </span><span class="keyword">&lt;tt&gt;</span><span class="comment">a</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> is </span><span class="keyword">&lt;tt&gt;</span><span class="comment">null</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IndexOutOfBoundsException unless </span><span class="keyword">&lt;tt&gt;</span><span class="comment">(0 &lt;= lo) &amp;&amp; (lo &lt;= hi) &amp;&amp; (hi &lt; a.length)</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     * </span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">shuffle</span><span class="symbol">(</span><span class="normal">Object</span><span class="symbol">[]</span><span class="normal"> a</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> lo</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> hi</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">a </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">(</span><span class="string">"argument array is null"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">lo </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> lo </span><span class="symbol">&gt;</span><span class="normal"> hi </span><span class="symbol">||</span><span class="normal"> hi </span><span class="symbol">&gt;=</span><span class="normal"> a</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IndexOutOfBoundsException</span><span class="symbol">(</span><span class="string">"Illegal subarray range"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> lo</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;=</span><span class="normal"> hi</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> r </span><span class="symbol">=</span><span class="normal"> i </span><span class="symbol">+</span><span class="normal"> </span><span class="function">uniform</span><span class="symbol">(</span><span class="normal">hi</span><span class="symbol">-</span><span class="normal">i</span><span class="symbol">+</span><span class="number">1</span><span class="symbol">);</span><span class="normal">     </span><span class="comment">// between i and hi</span>
<span class="normal">            </span><span class="usertype">Object</span><span class="normal"> temp </span><span class="symbol">=</span><span class="normal"> a</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">];</span>
<span class="normal">            a</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> a</span><span class="symbol">[</span><span class="normal">r</span><span class="symbol">];</span>
<span class="normal">            a</span><span class="symbol">[</span><span class="normal">r</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> temp</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Rearranges the elements of the specified subarray in uniformly random order.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  a the array to shuffle</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  lo the left endpoint (inclusive)</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  hi the right endpoint (inclusive)</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> NullPointerException if </span><span class="keyword">&lt;tt&gt;</span><span class="comment">a</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> is </span><span class="keyword">&lt;tt&gt;</span><span class="comment">null</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IndexOutOfBoundsException unless </span><span class="keyword">&lt;tt&gt;</span><span class="comment">(0 &lt;= lo) &amp;&amp; (lo &lt;= hi) &amp;&amp; (hi &lt; a.length)</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">shuffle</span><span class="symbol">(</span><span class="type">double</span><span class="symbol">[]</span><span class="normal"> a</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> lo</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> hi</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">a </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">(</span><span class="string">"argument array is null"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">lo </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> lo </span><span class="symbol">&gt;</span><span class="normal"> hi </span><span class="symbol">||</span><span class="normal"> hi </span><span class="symbol">&gt;=</span><span class="normal"> a</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IndexOutOfBoundsException</span><span class="symbol">(</span><span class="string">"Illegal subarray range"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> lo</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;=</span><span class="normal"> hi</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> r </span><span class="symbol">=</span><span class="normal"> i </span><span class="symbol">+</span><span class="normal"> </span><span class="function">uniform</span><span class="symbol">(</span><span class="normal">hi</span><span class="symbol">-</span><span class="normal">i</span><span class="symbol">+</span><span class="number">1</span><span class="symbol">);</span><span class="normal">     </span><span class="comment">// between i and hi</span>
<span class="normal">            </span><span class="type">double</span><span class="normal"> temp </span><span class="symbol">=</span><span class="normal"> a</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">];</span>
<span class="normal">            a</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> a</span><span class="symbol">[</span><span class="normal">r</span><span class="symbol">];</span>
<span class="normal">            a</span><span class="symbol">[</span><span class="normal">r</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> temp</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Rearranges the elements of the specified subarray in uniformly random order.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  a the array to shuffle</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  lo the left endpoint (inclusive)</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  hi the right endpoint (inclusive)</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> NullPointerException if </span><span class="keyword">&lt;tt&gt;</span><span class="comment">a</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> is </span><span class="keyword">&lt;tt&gt;</span><span class="comment">null</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IndexOutOfBoundsException unless </span><span class="keyword">&lt;tt&gt;</span><span class="comment">(0 &lt;= lo) &amp;&amp; (lo &lt;= hi) &amp;&amp; (hi &lt; a.length)</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">shuffle</span><span class="symbol">(</span><span class="type">int</span><span class="symbol">[]</span><span class="normal"> a</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> lo</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> hi</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">a </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">(</span><span class="string">"argument array is null"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">lo </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> lo </span><span class="symbol">&gt;</span><span class="normal"> hi </span><span class="symbol">||</span><span class="normal"> hi </span><span class="symbol">&gt;=</span><span class="normal"> a</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IndexOutOfBoundsException</span><span class="symbol">(</span><span class="string">"Illegal subarray range"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> lo</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;=</span><span class="normal"> hi</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> r </span><span class="symbol">=</span><span class="normal"> i </span><span class="symbol">+</span><span class="normal"> </span><span class="function">uniform</span><span class="symbol">(</span><span class="normal">hi</span><span class="symbol">-</span><span class="normal">i</span><span class="symbol">+</span><span class="number">1</span><span class="symbol">);</span><span class="normal">     </span><span class="comment">// between i and hi</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> temp </span><span class="symbol">=</span><span class="normal"> a</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">];</span>
<span class="normal">            a</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> a</span><span class="symbol">[</span><span class="normal">r</span><span class="symbol">];</span>
<span class="normal">            a</span><span class="symbol">[</span><span class="normal">r</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> temp</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Unit test.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">main</span><span class="symbol">(</span><span class="normal">String</span><span class="symbol">[]</span><span class="normal"> args</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> n </span><span class="symbol">=</span><span class="normal"> Integer</span><span class="symbol">.</span><span class="function">parseInt</span><span class="symbol">(</span><span class="normal">args</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">]);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">args</span><span class="symbol">.</span><span class="normal">length </span><span class="symbol">==</span><span class="normal"> </span><span class="number">2</span><span class="symbol">)</span><span class="normal"> StdRandom</span><span class="symbol">.</span><span class="function">setSeed</span><span class="symbol">(</span><span class="normal">Long</span><span class="symbol">.</span><span class="function">parseLong</span><span class="symbol">(</span><span class="normal">args</span><span class="symbol">[</span><span class="number">1</span><span class="symbol">]));</span>
<span class="normal">        </span><span class="type">double</span><span class="symbol">[]</span><span class="normal"> probabilities </span><span class="symbol">=</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span><span class="number">0.5</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0.3</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0.1</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0.1</span><span class="normal"> </span><span class="cbracket">}</span><span class="symbol">;</span>
<span class="normal">        </span><span class="type">int</span><span class="symbol">[]</span><span class="normal"> frequencies </span><span class="symbol">=</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span><span class="number">5</span><span class="symbol">,</span><span class="normal"> </span><span class="number">3</span><span class="symbol">,</span><span class="normal"> </span><span class="number">1</span><span class="symbol">,</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="cbracket">}</span><span class="symbol">;</span>
<span class="normal">        String</span><span class="symbol">[]</span><span class="normal"> a </span><span class="symbol">=</span><span class="normal"> </span><span class="string">"A B C D E F G"</span><span class="symbol">.</span><span class="function">split</span><span class="symbol">(</span><span class="string">" "</span><span class="symbol">);</span>

<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"seed = "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> StdRandom</span><span class="symbol">.</span><span class="function">getSeed</span><span class="symbol">());</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> n</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            StdOut</span><span class="symbol">.</span><span class="function">printf</span><span class="symbol">(</span><span class="string">"%2d "</span><span class="normal">  </span><span class="symbol">,</span><span class="normal"> </span><span class="function">uniform</span><span class="symbol">(</span><span class="number">100</span><span class="symbol">));</span>
<span class="normal">            StdOut</span><span class="symbol">.</span><span class="function">printf</span><span class="symbol">(</span><span class="string">"%8.5f "</span><span class="symbol">,</span><span class="normal"> </span><span class="function">uniform</span><span class="symbol">(</span><span class="number">10.0</span><span class="symbol">,</span><span class="normal"> </span><span class="number">99.0</span><span class="symbol">));</span>
<span class="normal">            StdOut</span><span class="symbol">.</span><span class="function">printf</span><span class="symbol">(</span><span class="string">"%5b "</span><span class="normal">  </span><span class="symbol">,</span><span class="normal"> </span><span class="function">bernoulli</span><span class="symbol">(</span><span class="number">0.5</span><span class="symbol">));</span>
<span class="normal">            StdOut</span><span class="symbol">.</span><span class="function">printf</span><span class="symbol">(</span><span class="string">"%7.5f "</span><span class="symbol">,</span><span class="normal"> </span><span class="function">gaussian</span><span class="symbol">(</span><span class="number">9.0</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0.2</span><span class="symbol">));</span>
<span class="normal">            StdOut</span><span class="symbol">.</span><span class="function">printf</span><span class="symbol">(</span><span class="string">"%1d "</span><span class="normal">  </span><span class="symbol">,</span><span class="normal"> </span><span class="function">discrete</span><span class="symbol">(</span><span class="normal">probabilities</span><span class="symbol">));</span>
<span class="normal">            StdOut</span><span class="symbol">.</span><span class="function">printf</span><span class="symbol">(</span><span class="string">"%1d "</span><span class="normal">  </span><span class="symbol">,</span><span class="normal"> </span><span class="function">discrete</span><span class="symbol">(</span><span class="normal">frequencies</span><span class="symbol">));</span>
<span class="normal">            StdRandom</span><span class="symbol">.</span><span class="function">shuffle</span><span class="symbol">(</span><span class="normal">a</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">String</span><span class="normal"> s </span><span class="symbol">:</span><span class="normal"> a</span><span class="symbol">)</span>
<span class="normal">                StdOut</span><span class="symbol">.</span><span class="function">print</span><span class="symbol">(</span><span class="normal">s</span><span class="symbol">);</span>
<span class="normal">            StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="cbracket">}</span>

<span class="comment">/******************************************************************************</span>
<span class="comment"> *  Copyright 2002-2015, Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  This file is part of algs4.jar, which accompanies the textbook</span>
<span class="comment"> *</span>
<span class="comment"> *      Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,</span>
<span class="comment"> *      Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.</span>
<span class="comment"> *      </span><span class="url">http://algs4.cs.princeton.edu</span>
<span class="comment"> *</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is free software: you can redistribute it and/or modify</span>
<span class="comment"> *  it under the terms of the GNU General Public License as published by</span>
<span class="comment"> *  the Free Software Foundation, either version 3 of the License, or</span>
<span class="comment"> *  (at your option) any later version.</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is distributed in the hope that it will be useful,</span>
<span class="comment"> *  but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="comment"> *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="comment"> *  GNU General Public License for more details.</span>
<span class="comment"> *</span>
<span class="comment"> *  You should have received a copy of the GNU General Public License</span>
<span class="comment"> *  along with algs4.jar.  If not, see </span><span class="url">http://www.gnu.org/licenses.</span>
<span class="comment"> ******************************************************************************/</span>
</tt></pre>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10811519-2");
pageTracker._trackPageview();
} catch(err) {}</script>

</body>

<p><br><address><small>
Last updated: Mon Mar 21 10:51:08 EDT 2016.
</small></address>

</html>
